package com.dykj.apps.web.sysmanager.dao;


import java.util.List;

import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.dao.AbstractPublicDAO;

public class GroupDAOImpl extends AbstractPublicDAO implements IGroupDAO {

	public List findAllGroupId() throws DatabaseException {
		String sql = "select s.groupid,s.parentid,s.name from SysGroup s";
		List list = super.queryJDBC(sql);
		return list;
	}
	public List findAllParent(String id) throws DatabaseException{
		String hql;
		if(id==null||"".equals(id)){
			hql = "from SysGroup s ";
		}else{
		hql = "from SysGroup s where s.groupid <>'"+id+"'";
		}
		List list = super.query(hql);		
		return list;
	}

	public List findGroupInfoById(String parentid) throws DatabaseException {
		String hql;
		if(parentid==null||"".equals(parentid)){
			//hql = "from SysGroup s where s.parentid is null";
			hql = "from SysGroup s where s.parentid is null or s.parentid = '191'";
		}else{
			hql = "from SysGroup s where s.parentid = '"+parentid+"'";
		}
		List list = super.query(hql);
		return list;
	}

	public int getGroupCout(String parentid) throws DatabaseException {
		String hql;
		if(parentid==null||"".equals(parentid)){
			hql = "select count(*) from SysGroup s where s.parentid is null";
		}else{
			hql = "select count(*) from SysGroup s where s.parentid = '"+parentid+"'";
		}
		List list = super.query(hql);
		if(list.size()>0)
			return Integer.parseInt(list.get(0).toString());
		else
			return 0;
	}
	public List findHaveUser(String id) throws DatabaseException {
		String hql;
			hql = "from SysUser s where s.groupid = '"+ id +"'";
		List list = super.query(hql);
		return list;
	}
	
	/**
	 * 机构修改时, 批量修改 经办机构编号
	 * @param sourceOrg 原来 经办机构编号
	 * @param thisOrg   现在 经办机构编号
	 * @return 错误整型值
	 * @throws DatabaseException
	 */
	public int updateSysConOrgToThis(String sourceOrg,String thisOrg) throws DatabaseException
	{
		int tempfile = this.execDML("update Tempfile t set t.groupid='"+thisOrg+"' where t.groupid='"+sourceOrg+"'");
		int oddtempfile = this.execDML("update OddTempfile o set o.groupid='"+thisOrg+"' where o.groupid='"+sourceOrg+"'");
		int areal = this.execDML("update Datareal a set a.groupid='"+thisOrg+"' where a.groupid='"+sourceOrg+"'");
		return tempfile + oddtempfile + areal; 
	}	
}
